package com.zyd.blog.personal.service.impl;
import	java.util.HashMap;

import com.zyd.blog.persistence.mapper.UserApplyDesignTempMapper;
import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.enuminterface.Step;
import com.zyd.blog.personal.enuminterface.UserType;
import com.zyd.blog.personal.service.UserApplyDesignTempService;
import com.zyd.blog.personal.service.UserCompanyService;
import com.zyd.blog.personal.service.UserInfoService;
import com.zyd.blog.personal.service.UserNoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户申请设计师业务逻辑层
 * @author mfy
 */
@Service
public class UserApplyDesignTempServiceImpl implements UserApplyDesignTempService {

    @Resource
    private UserApplyDesignTempMapper userApplyDesignTempMapper;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private UserNoService userNoService;

    @Resource
    private UserCompanyService userCompanyService;

    @Override
    public Boolean isHaveUserApplyDesignTemp(Long userId){
        if(userId == null){
            return false;
        }
        Map<String,Object> userApplyDesignTemp = getUserApplyDesignTemp(userId);
        return !(userApplyDesignTemp == null || userApplyDesignTemp.size() == 0);
    }

    @Override
    public Map<String,Object> getUserApplyDesignTemp(Long userId){
        if(userId == null){
            return new HashMap<>(0);
        }
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("userId",userId);
        condition.put("isDelete", IsDelete.IS_DELETE_NO);
        List<Map<String,Object>> userApplyDesignTemp = userApplyDesignTempMapper.queryApplyDesignTempByCondition(condition);
        if(userApplyDesignTemp == null || userApplyDesignTemp.size() == 0){
            return new HashMap<>(0);
        }
        return userApplyDesignTemp.get(0);
    }

    @Override
    public List<Map<String,Object>> getAllUserApplyDesignInfo(Map<String,Object> map){
        return userApplyDesignTempMapper.queryApplyDesignTempByCondition(map);
    }

    @Override
    public Boolean addUserApplyDesignTemp(Map<String,Object> map){
        map.put("updateTime",(new Date()).getTime());
        map.put("addTime",(new Date()).getTime());
        map.put("isSubmit","Y");
        map.put("submitTime",(new Date()).getTime());
        return userApplyDesignTempMapper.addApplyDesignTemp(map) > 0;
    }

    @Override
    public Boolean updateUserApplyDesignTemp(Map<String,Object> map){
        Long userId = (Long) map.get("userId");
        Long id = (Long) map.get("id");
        if(userId == null && id == null){
            return false;
        }
        map.put("reviewTime",(new Date()).getTime());
        map.put("updateTime",(new Date()).getTime());
        map.put("isSubmit",(new Date()).getTime());
        map.put("submitTime",(new Date()).getTime());
        return userApplyDesignTempMapper.updateApplyDesignTemp(map) > 0;
    }


    /**
     * 修改用户基本信息
     * @param userApplyDesignInfo   临时信息
     * @return  结果
     */
    private Boolean updateUserNo(Map<String,Object> userApplyDesignInfo){
        Map<String,Object> userNoCondition = new HashMap<>(16);
        userNoCondition.put("id",userApplyDesignInfo.get("userId"));
        userNoCondition.put("username",userApplyDesignInfo.get("username"));
        userNoCondition.put("reviewName",userApplyDesignInfo.get("reviewName"));
        userNoCondition.put("reviewTime",userApplyDesignInfo.get("reviewTime"));
        userNoCondition.put("designTime",userApplyDesignInfo.get("designTime"));
        userNoCondition.put("userType",UserType.USER_TYPE_DESIGNER);
        userNoCondition.put("step",Step.STEP_REVIEW_YES);
        userNoCondition.put("reason", "审核通过");
        return userNoService.updateUserNo(userNoCondition);
    }


    /**
     * 修改用户详细信息
     * @param userApplyDesignInfo   临时信息
     * @return  结果
     */
    private Boolean updateUserInfo(Map<String,Object> userApplyDesignInfo){
        Map<String,Object> userInfoCondition = new HashMap<>(16);
        Long userId = (Long) userApplyDesignInfo.get("userId");
        userInfoCondition.putAll(userApplyDesignInfo);
        userInfoCondition.put("nickname",userApplyDesignInfo.get("username"));
        userInfoCondition.put("homeAddress",userApplyDesignInfo.get("liveAddress"));
        boolean result ;
        if(userInfoService.isHaveUserInfo(userId)){
            result = userInfoService.updateUserInfo(userInfoCondition);
        }else{
            result = userInfoService.addUserInfo(userInfoCondition);
        }
        return result;
    }

    /**
     * 修改用户详细信息
     * @param userApplyDesignInfo   临时信息
     * @return  结果
     */
    private Boolean updateUserCompany(Map<String,Object> userApplyDesignInfo){
        Map<String,Object> userCompanyCondition = new HashMap<>(16);
        Long userId = (Long) userApplyDesignInfo.get("userId");
        userCompanyCondition.putAll(userApplyDesignInfo);
        userCompanyCondition.put("workType",userApplyDesignInfo.get("designType"));
        userCompanyCondition.put("duty",userApplyDesignInfo.get("designType"));
        userCompanyCondition.put("teamName",userApplyDesignInfo.get("companyName"));
        userCompanyCondition.put("companyAddress",userApplyDesignInfo.get("workAddress"));
        userCompanyCondition.put("joinTime",userApplyDesignInfo.get("designTime"));
        boolean result ;
        if(userCompanyService.isHaveUserCompany(userId)){
            result = userCompanyService.updateCompany(userCompanyCondition);
        }else{
            result = userCompanyService.addUserCompanyInfo(userCompanyCondition);
        }
        return result;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean reViewApplyDesign(Long id,Long userId,Integer result,String reason,String reviewName){
        if(userId == null){
            return false;
        }
        if(result == null){
            return false;
        }
        switch (result){
            case 1:
                //  审核成功
                Map<String,Object> userApplyDesignInfo = getUserApplyDesignTemp(userId);
                if(userApplyDesignInfo == null || userApplyDesignInfo.size() == 0){
                    return false;
                }
                Map<String,Object> userApplyDesignTempCondition1 = new HashMap<>(16);
                userApplyDesignTempCondition1.put("id", id);
                userApplyDesignTempCondition1.put("step",Step.STEP_REVIEW_YES);
                userApplyDesignTempCondition1.put("reason","审核通过");
                userApplyDesignTempCondition1.put("reviewName",reviewName);
                if(updateUserApplyDesignTemp(userApplyDesignTempCondition1)){
                    userApplyDesignInfo.remove("id");
                    userApplyDesignInfo.remove("updateTime");
                    userApplyDesignInfo.remove("addTime");
                    userApplyDesignInfo.remove("isSubmit");
                    userApplyDesignInfo.remove("submitTime");
                    userApplyDesignInfo.put("reviewName",reviewName);
                    userApplyDesignInfo.put("reviewTime",(new Date()).getTime());
                    if(updateUserInfo(userApplyDesignInfo) && updateUserCompany(userApplyDesignInfo)){
                        return updateUserNo(userApplyDesignInfo);
                    }
                }
            case 2 :
                //  审核失败
                Map<String,Object> userApplyDesignTempCondition = new HashMap<>(16);
                userApplyDesignTempCondition.put("id", id);
                userApplyDesignTempCondition.put("step",Step.STEP_REVIEW_NO);
                userApplyDesignTempCondition.put("reason",reason);
                userApplyDesignTempCondition.put("reviewName",reviewName);
                userApplyDesignTempCondition.put("reviewTime",(new Date()).getTime());
                if(updateUserApplyDesignTemp(userApplyDesignTempCondition)){
                    Map<String,Object> userNoCondition = new HashMap<>(16);
                    userNoCondition.put("id", userId);
                    userNoCondition.put("step", Step.STEP_REVIEW_NO);
                    userNoCondition.put("reason", reason);
                    userNoCondition.put("reviewName",reviewName);
                    userNoCondition.put("reviewTime",(new Date()).getTime());
                    return userNoService.updateUserNo(userNoCondition);
                }
            default :
                break;
        }
        return false;
    }

    @Override
    public boolean deleteReconding(Long id){
        if(id == null){
            return false;
        }
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("id",id);
        condition.put("isDelete",IsDelete.IS_DELETE_YES);
        return userApplyDesignTempMapper.updateApplyDesignTemp(condition) > 0;
    }
}
